package com.zzx.business.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzx.business.domain.BusinessSite;
import com.zzx.business.service.SiteService;
import com.zzx.common.annotation.Log;
import com.zzx.common.core.controller.BaseController;
import com.zzx.common.core.domain.AjaxResult;
import com.zzx.common.enums.BusinessType;
import com.zzx.common.utils.SecurityUtils;
import com.zzx.common.utils.poi.ExcelUtil;
import com.zzx.business.vo.BusinessSiteVo;
import com.zzx.business.service.IBusinessSiteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.zzx.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 站点信息Controller
 *
 * @author Z、zx
 * @date 2024-08-21
 */
@Api(tags = "站点信息Controller")
@RestController
@RequestMapping("/business/site")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class BusinessSiteController extends BaseController {

    private final IBusinessSiteService businessSiteService;
    private final SiteService siteService;

    @ApiOperation("查询站点信息列表")
    @PreAuthorize("@ss.hasPermi('business:site:list')")
    @GetMapping("/list")
    public TableDataInfo<BusinessSiteVo> list(BusinessSiteVo entity) {
        return businessSiteService.queryList(entity);
    }

    @ApiOperation("查询站点信息所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(BusinessSiteVo entity) {
        Long deptId = SecurityUtils.getDeptId();
        BusinessSite one = businessSiteService.getOne(Wrappers.<BusinessSite>lambdaQuery().eq(BusinessSite::getDeptId, deptId));
        if(one != null){
            String type = entity.getType();
            if("1".equals(type)){
                entity.setCode(one.getCode());
            } else {
                entity.setCode(null);
            }
        }
        return AjaxResult.success("查询成功", businessSiteService.queryAll(entity));
    }

    @ApiOperation("导出站点信息列表")
    @PreAuthorize("@ss.hasPermi('business:site:export')")
    @Log(title = "站点信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusinessSiteVo entity) {
        List<BusinessSiteVo> list = businessSiteService.queryAll(entity);
        ExcelUtil<BusinessSiteVo> util = new ExcelUtil<>(BusinessSiteVo.class);
        util.exportExcel(response, list, "站点信息数据");
    }

    @ApiOperation("获取站点信息详细信息")
    @PreAuthorize("@ss.hasPermi('business:site:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", businessSiteService.queryById(id));
    }

    @ApiOperation("新增站点信息")
    @PreAuthorize("@ss.hasPermi('business:site:add')")
    @Log(title = "站点信息", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody BusinessSiteVo entity) {
        return toAjax(siteService.save(entity));
    }

    @ApiOperation("修改站点信息")
    @PreAuthorize("@ss.hasPermi('business:site:edit')")
    @Log(title = "站点信息", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody BusinessSiteVo entity) {
        return toAjax(siteService.updateById(entity));
    }

    @ApiOperation("删除站点信息")
    @PreAuthorize("@ss.hasPermi('business:site:remove')")
    @Log(title = "站点信息", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(siteService.removeByIds(Arrays.asList(ids)));
    }
}
